local que_eqi = fk.CreateSkill {
    name = "que_eqi",
}
Fk:loadTranslationTable {
    ["que_eqi"] = "恶契",
    [":que_eqi"] = "每回合限一次，出牌阶段或当你受到其他角色造成的伤害后，你可以从牌堆或弃牌堆中获得一张点数为X，且字数为你体力值的牌。<br>①：若你没有获得牌且当前是你的回合，" ..
        "你可以对一名角色造成1点毒素伤害，然后将手牌调整至体力上限。<br>②：若此时是你的回合外，你令当前回合角色将手牌调整至体力值，" ..
        "若其因此获得或弃置的牌中，有任何一项符合本次发动条件，你获得之(X为当前角色手牌数，至少为1)。",

    ["#que_eqi"] = "恶契：是否获得一张点数为 %arg 且字数为 %arg2 的牌？",
}
que_eqi:addEffect('active', {
    anim_type = "drawcard",
    prompt = function(self, player)
        local n = player:getHandcardNum()
        return "#que_eqi:::" .. n .. ":" .. player.hp
    end,
    max_card_num = 0,
    target_num = 0,
    can_use = function(self, player)
        return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
    end,
    card_filter = Util.FalseFunc,
    target_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = effect.from
        local x = math.max(1, player:getHandcardNum())
        local n = player.hp
        local cards = table.filter(table.connect(room.draw_pile, room.discard_pile), function(id)
            return Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len() == n and Fk:getCardById(id).number == x
        end)
        local card
        if #cards > 0 then
            card = table.random(cards)
            card = Fk:getCardById(card)
            room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, que_eqi.name, nil, false, player)
        else
            local targets = table.filter(room:getOtherPlayers(player), function(p)
                return not p.dead
            end)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = targets,
                skill_name = que_eqi.name,
                prompt = "恶契：请选择一名其他角色，对其造成一点毒素伤害",
                cancelable = true,
            })
            if #to > 0 then
                room:damage {
                    from = player,
                    to = to[1],
                    damage = 1,
                    damageType = fk.wdPoisonDamage,
                    skillName = que_eqi.name,
                }
            end
            if player.maxHp == player:getHandcardNum() then return end
            if player:getHandcardNum() < player.maxHp then
                player:drawCards(player.maxHp - player:getHandcardNum(), que_eqi.name)
            else
                room:askToDiscard(player, {
                    min_num = player:getHandcardNum() - player.maxHp,
                    max_num = player:getHandcardNum() - player.maxHp,
                    include_equip = false,
                    cancelable = false,
                    skill_name = que_eqi.name,
                })
            end
        end
    end,
})

que_eqi:addEffect(fk.Damaged, {
    anim_type = "defensive",
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(que_eqi.name) and target == player and data.from and data.from ~= player and
            player:usedSkillTimes(que_eqi.name, Player.HistoryTurn) == 0
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local x
        if room.current == player then
            x = math.max(1, player:getHandcardNum())
        else
            x = math.max(1, data.from:getHandcardNum())
        end
        local n = player.hp
        return room:askToSkillInvoke(player, { skill_name = que_eqi.name, prompt = "#que_eqi:::" .. x .. ":" .. n })
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local x
        if room.current == player then
            x = math.max(1, player:getHandcardNum())
        else
            x = math.max(1, data.from:getHandcardNum())
        end
        local n = player.hp
        local cards = table.filter(table.connect(room.draw_pile, room.discard_pile), function(id)
            return Fk:translate(Fk:getCardById(id).trueName, "zh_CN"):len() == n and Fk:getCardById(id).number == x
        end)
        local card
        if #cards > 0 then
            card = table.random(cards)
            card = Fk:getCardById(card)
            room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, que_eqi.name, nil, false, player)
        end
        if room.current ~= player and data.from then
            local num = data.from:getHandcardNum()
            local maxhp = data.from.hp
            --room:loseHp(data.from, 1, que_eqi.name)
            if maxhp == num then return end
            local givecards = {}
            local ids
            if num < maxhp then
                ids = data.from:drawCards(maxhp - num, que_eqi.name)
            else
                ids = room:askToDiscard(data.from, {
                    min_num = num - maxhp,
                    max_num = num - maxhp,
                    include_equip = false,
                    cancelable = false,
                    skill_name = que_eqi.name,
                })
            end
            for _, id in ipairs(ids) do
                local i = Fk:getCardById(id)
                if i.number == x or Fk:translate(i.trueName, "zh_CN"):len() == n then
                    table.insert(givecards, i)
                end
            end
            if #givecards > 0 then
                room:moveCardTo(givecards, Card.PlayerHand, player, fk.ReasonJustMove, que_eqi.name, nil, false, player)
            end
        end
    end,
})
return que_eqi
